// 注册接口 - 使用假数据
export default defineEventHandler(async (event) => {
  const body = await readBody(event)
  const { username, email, password } = body

  // 模拟延迟
  await new Promise(resolve => setTimeout(resolve, 800))

  // 简单验证
  if (!username || username.length < 3) {
    throw createError({
      statusCode: 400,
      message: '用户名至少需要3个字符'
    })
  }

  if (!email || !email.includes('@')) {
    throw createError({
      statusCode: 400,
      message: '请输入有效的邮箱地址'
    })
  }

  if (!password || password.length < 6) {
    throw createError({
      statusCode: 400,
      message: '密码至少需要6个字符'
    })
  }

  // 检查邮箱是否已存在（模拟）
  const existingEmails = ['admin@example.com', 'test@example.com']
  if (existingEmails.includes(email)) {
    throw createError({
      statusCode: 409,
      message: '该邮箱已被注册'
    })
  }

  // 创建新用户（模拟）
  const newUser = {
    id: String(Math.floor(Math.random() * 10000) + 100),
    username,
    email,
    avatar: `https://api.dicebear.com/7.x/avataaars/svg?seed=${username}`,
    role: 'user',
    createdAt: new Date().toISOString()
  }

  // 生成假的 token
  const token = `token_${newUser.id}_${Date.now()}`
  const refreshToken = `refresh_${newUser.id}_${Date.now()}`

  return {
    code: 200,
    message: '注册成功',
    data: {
      token,
      refreshToken,
      user: {
        id: newUser.id,
        username: newUser.username,
        email: newUser.email,
        avatar: newUser.avatar,
        role: newUser.role
      }
    }
  }
})

